package xyz.yuzegod.megawalls.inventory;

import fr.minuskube.inv.ClickableItem;
import fr.minuskube.inv.SmartInventory;
import fr.minuskube.inv.content.InventoryContents;
import fr.minuskube.inv.content.InventoryProvider;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryType;
import xyz.yuzegod.megawalls.MegaWalls;
import xyz.yuzegod.megawalls.classes.Classes;
import xyz.yuzegod.megawalls.classes.ClassesManager;
import xyz.yuzegod.megawalls.classes.Skill;
import xyz.yuzegod.megawalls.game.GamePlayer;
import xyz.yuzegod.megawalls.stats.KitStatsContainer;
import xyz.yuzegod.megawalls.util.ItemBuilder;
import xyz.yuzegod.megawalls.util.StringUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ClassesContainer {
     public static SmartInventory build() {
          final SmartInventory.Builder builder = SmartInventory.builder(MegaWalls.getInstance().getInventoryManager());
          builder.title("职业收集");
          builder.type(InventoryType.CHEST);
          builder.size(6, 9);
          builder.closeable(true);
          builder.parent(InventoryManager.SHOPMENU);
          builder.provider(new InventoryProvider() {
               public void init(Player player, InventoryContents contents) {
                    GamePlayer gamePlayer = GamePlayer.get(player.getUniqueId());
                    int row = 1;
                    int column = 1;
                    List classesList = ClassesManager.sort(gamePlayer.getPlayerStats().unlockedClasses());
                    Iterator var7 = classesList.iterator();

                    while(var7.hasNext()) {
                         Classes classes = (Classes)var7.next();
                         if (gamePlayer.getPlayerStats().isUnlocked(classes)) {
                              KitStatsContainer kitStats = gamePlayer.getPlayerStats().getKitStats(classes);
                              ItemBuilder itemBuilder = (new ItemBuilder(classes.getIconType(), 1, classes.getIconData())).setDisplayName("§a" + classes.getDisplayName() + ClassesContainer.getMasterStar(kitStats.getLevel()));
                              List lore = new ArrayList();
                              lore.add("§8" + classes.getClassesType().getName() + "职业");
                              lore.add("§7职业定位:" + classes.getOrientations()[0].getText() + " " + classes.getOrientations()[1].getText());
                              lore.add("§7难度:" + classes.getDifficulty().getText());
                              lore.add(" ");
                              lore.add("§7升级进度:" + kitStats.upgradePercent());
                              lore.add(ClassesContainer.getUpgradeBar((String)(classes.getDisplayName() + "职业套装"), kitStats, kitStats.getEquipLevel(), 5));
                              lore.add(ClassesContainer.getUpgradeBar((Skill)classes.getMainSkill(), kitStats, kitStats.getSkillLevel(), 5));
                              lore.add(ClassesContainer.getUpgradeBar((Skill)classes.getSecondSkill(), kitStats, kitStats.getSkill2Level(), 3));
                              lore.add(ClassesContainer.getUpgradeBar((Skill)classes.getThirdSkill(), kitStats, kitStats.getSkill3Level(), 3));
                              lore.add(ClassesContainer.getUpgradeBar((Skill)classes.getCollectSkill(), kitStats, kitStats.getSkill4Level(), 3));
                              lore.add(" ");
                              lore.add("§7末影箱:§a" + kitStats.getEnderChest() + "行");
                              lore.add(" ");
                              lore.add("§e点击查看！");
                              itemBuilder.setLore((List)lore);
                              contents.set(row, column, ClickableItem.of(itemBuilder.build(), (e) -> {
                                   UpgradeMenu.menu(classes, contents.inventory()).open(player);
                              }));
                              if (column + 1 > 7) {
                                   ++row;
                                   column = 1;
                              } else {
                                   ++column;
                              }
                         }
                    }

                    contents.set(5, 3, ClickableItem.of((new ItemBuilder(Material.ARROW)).setDisplayName("§a返回").setLore("§7至" + builder.getParent().getTitle()).build(), (e) -> {
                         builder.getParent().open(player);
                    }));
                    contents.set(5, 4, ClickableItem.of((new ItemBuilder(Material.EMERALD)).setDisplayName("§7总硬币:§6" + StringUtils.formattedCoins(gamePlayer.getPlayerStats().getCoins())).build(), (e) -> {
                    }));
               }

               public void update(Player player, InventoryContents contents) {
               }
          });
          return builder.build();
     }

     public static String getUpgradeBar(Skill skill, KitStatsContainer kitStats, int level, int max) {
          return StringUtils.upgradeBar(level, max) + " §7" + skill.getName();
     }

     public static String getUpgradeBar(String name, KitStatsContainer kitStats, int level, int max) {
          return StringUtils.upgradeBar(level, max) + " §7" + name;
     }

     public static String getMasterStar(int level) {
          if (level < 2) {
               return "";
          } else {
               StringBuffer sb = new StringBuffer(" §6");

               for(int i = 2; i <= level; ++i) {
                    sb.append("✫");
               }

               return sb.toString();
          }
     }
}
